﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using System.IO;
using Microsoft.Xna.Framework;
using System.Reflection;

namespace Aim2 {
    public class AudioManager : GameComponent {
        private static AudioManager audioManager = null;
        private FileInfo[] audioFileList;
        private DirectoryInfo audioFolder;
        private static Dictionary<string, SoundEffect> soundList;

        public static Song CustomTrack { get; set; }
        private static Song menuTrack;

        static Game game;

        #region Initialization Methods

        private AudioManager(Game game, DirectoryInfo audioDirectory)
            : base(game) {
                AudioManager.game = game;
            try {
                audioFolder = audioDirectory;
                audioFileList = audioFolder.GetFiles("*.xnb");
                soundList = new Dictionary<string, SoundEffect>();

                for (int i = 0; i < audioFileList.Length; i++) {
                    string soundName = Path.GetFileNameWithoutExtension(audioFileList[i].Name);
                    soundList[soundName] = game.Content.Load<SoundEffect>("audio\\wav\\" + soundName);
                    soundList[soundName].Name = soundName;
                }

                menuTrack = game.Content.Load<Song>("audio\\mp3\\" + "strobe");
            }
            catch (NoAudioHardwareException) {
                // silently fall back to silence
            }
        }

        public static void Initialize(Game game, DirectoryInfo audioDirectory) {
            if (game == null)
                return;

            audioManager = new AudioManager(game, audioDirectory);
            game.Components.Add(audioManager);
        }

        public static void LoadCustomTrack(DirectoryInfo folder) {
            //string soundName = Path.GetFileNameWithoutExtension(folder.GetFiles("*.mp3")[0].Name);
            string soundName = Path.GetFileName(folder.GetFiles("*.mp3")[0].Name);
            //ok c'est un peu dégueu:
            var ctor = typeof(Song).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, 
                                                    null,
                                                    new[] { typeof(string), typeof(string), typeof(int) }, 
                                                    null);
            CustomTrack = (Song)ctor.Invoke(new object[] { soundName, folder.FullName + "\\" + soundName, 0 });
        }
        #endregion


        #region Sound Play Methods

        public static bool StopPlayingTrack() {
            var t = MediaPlayer.Volume;
            if (MediaPlayer.Volume > 0) {
                MediaPlayer.Volume -= 0.05f;
                return false;
            }
            MediaPlayer.Stop();
            return true;
        }

        public static void PlayCustomSoundTrack() {
            if (CustomTrack == null)
                return;
            MediaPlayer.Volume = 1.0f;
            MediaPlayer.Play(CustomTrack);
        }

        public static void PlayMenuTrack() {
            if (menuTrack == null)
                return;
            MediaPlayer.Volume = 1.0f;
            MediaPlayer.Play(menuTrack);
        }

        public static void PlaySoundEffect(string soundName) {
            if (audioManager == null || soundList == null)
                return;

            if (soundList.ContainsKey(soundName)) {
                soundList[soundName].Play();
            }
        }


        #endregion

    }
}
